package com.xw.hchat.service.impl;

import com.xw.hchat.mapper.TbFriendMapper;
import com.xw.hchat.mapper.TbFriendReqMapper;
import com.xw.hchat.mapper.TbUserMapper;
import com.xw.hchat.pojo.*;
import com.xw.hchat.pojo.vo.FriendReq;
import com.xw.hchat.pojo.vo.User;
import com.xw.hchat.service.FriendService;
import com.xw.hchat.util.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName FriendServiceImpl
 * @Author liu zhiyong
 * @Date 2020/8/19 9:36
 * @Version 1.0
 */
@Service
@Transactional
public class FriendServiceImpl implements FriendService {

    @Resource
    private TbFriendReqMapper friendReqMapper;
    @Resource
    private TbUserMapper userMapper;
    @Resource
    private TbFriendMapper friendMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public void sendRequest(String fromUserid, String toUserid) {
        /*
        检查是否添加好友
         */
        checkAllowAddFriend(fromUserid, userMapper.selectByPrimaryKey(toUserid));

        //添加好友请求
        TbFriendReq tbFriendReq = new TbFriendReq();
        tbFriendReq.setId(idWorker.nextId());
        tbFriendReq.setFromUserid(fromUserid);
        tbFriendReq.setToUserid(toUserid);
        tbFriendReq.setCreatetime(new Date());
        tbFriendReq.setStatus(0);

        //保存请求信息到数据库中
        friendReqMapper.insert(tbFriendReq);

    }



    /**
     * 检查是否允许添加好友
     * @Author liu zhiyong
     * @Date 2020/8/18 18:29
     * @Version 1.0
     * @param userid    要添加好友的用户id
     * @param friend    要添加的好友对象
     * @return void
     **/
    private void checkAllowAddFriend(String userid, TbUser friend) {
        //1.用户不能添加自己为好友
        if(userid.equals(friend.getId())){
            throw new RuntimeException("不能添加自己为好友");
        }
        //2.用户不能重复添加
        //如果该用户已经添加该好友，就不能再次添加
        TbFriendExample friendExample = new TbFriendExample();
        TbFriendExample.Criteria friendCriteria = friendExample.createCriteria();
        friendCriteria.andUseridEqualTo(userid);
        friendCriteria.andFriendsIdEqualTo(friend.getId());
        List<TbFriend> friendList = friendMapper.selectByExample(friendExample);
        if(friendList != null && friendList.size() > 0){
            throw new RuntimeException(friend.getUsername() + "已经是您的好友，不能再次添加");
        }
        //3.判断是否已经提交好友申请，如果已经提交好友申请，就直接抛出运行异常
        TbFriendReqExample friendReqExample = new TbFriendReqExample();
        TbFriendReqExample.Criteria friendReqExampleCriteria = friendReqExample.createCriteria();
        friendReqExampleCriteria.andFromUseridEqualTo(userid);
        friendReqExampleCriteria.andToUseridEqualTo(friend.getId());
        friendReqExampleCriteria.andStatusEqualTo(0);
        List<TbFriendReq> friendReqList = friendReqMapper.selectByExample(friendReqExample);
        if(friendReqList !=null && friendReqList.size() > 0){
            throw new RuntimeException(friend.getUsername() + "已经申请添加好友了，等候好友通过");
        }
    }

    public List<FriendReq> findFriendReqByUserid(String userid){

        /*
        根据用户id，查询对应的好友请求
         */
        TbFriendReqExample example = new TbFriendReqExample();
        TbFriendReqExample.Criteria criteria = example.createCriteria();
        criteria.andToUseridEqualTo(userid);
        criteria.andStatusEqualTo(0);
        List<TbFriendReq> tbFriendReqs = friendReqMapper.selectByExample(example);

        /*
        根据好友请求，将发起好友请求的用户信息返回
         */
        ArrayList<FriendReq> friendReqList = new ArrayList<>();

        for (TbFriendReq tbFriendReq : tbFriendReqs) {
            TbUser tbUser = userMapper.selectByPrimaryKey(tbFriendReq.getFromUserid());
            FriendReq friendReq = new FriendReq();
            BeanUtils.copyProperties(tbUser, friendReq);

            friendReq.setId(tbFriendReq.getId());//设置好友请求表的id
            friendReqList.add(friendReq);
        }

        return friendReqList;
    }

    @Override
    public void acceptFriendReq(String reqid) {
        /*
        1.将好友请求的status标志设置为1，表示已经处理了该好友请求
         */
        TbFriendReq friendReq = friendReqMapper.selectByPrimaryKey(reqid);
        friendReq.setStatus(1);
        friendReqMapper.updateByPrimaryKey(friendReq);

        /*
        2.互相添加好友（数据库中插入好友关系）
         */
        Date addDate = new Date();
        TbFriend friend1 = new TbFriend();
        friend1.setId(idWorker.nextId());
        friend1.setUserid(friendReq.getToUserid());
        friend1.setFriendsId(friendReq.getFromUserid());
        friend1.setCreatetime(addDate);


        TbFriend friend2 = new TbFriend();
        friend2.setId(idWorker.nextId());
        friend2.setUserid(friendReq.getFromUserid());
        friend2.setFriendsId(friendReq.getToUserid());
        friend2.setCreatetime(addDate);

        //插入到数据库
        friendMapper.insert(friend1);
        friendMapper.insert(friend2);
    }

    @Override
    public void ignoreFriendReq(String reqid) {
        /*
        将好友请求的status标志设置为1，表示已经处理了该好友请求
         */
        TbFriendReq friendReq = friendReqMapper.selectByPrimaryKey(reqid);
        friendReq.setStatus(1);
        friendReqMapper.updateByPrimaryKey(friendReq);
    }

    @Override
    public List<User> findFriendsByUserid(String userid) {

        /*
        根据当前登录的用户id，查询tb_friend好友表中的数据
         */
        TbFriendExample example = new TbFriendExample();
        TbFriendExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(userid);
        List<TbFriend> friendList = friendMapper.selectByExample(example);

        /*
        根据好友列表，查出对应好友的用户信息
         */
        ArrayList<User> users = new ArrayList<>();
        for (TbFriend tbFriend : friendList) {
            TbUser tbUser = userMapper.selectByPrimaryKey(tbFriend.getFriendsId());
            User user = new User();
            BeanUtils.copyProperties(tbUser, user);
            users.add(user);
        }

        return users;
    }
}
